//分页start
$(function (){
    Pointer = 0
    pager_number = 1
    data_for_one_num = 10
    page_data_url = baseURL +'/auth/api/dic/searchDic'
    authorities = $.session.get('authorities');
    function append(dicvalue,dicgroup) {
        $("#data-list").empty();
        //ajax start
        $.ajax({
            url: page_data_url,
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                //offset:起始位置 = 指针
                //limit:获取条数 =默认为10
                limit: data_for_one_num,
                offset: Pointer,
                dicvalue:dicvalue,
                dicgroup:dicgroup,
            },
            success: function (c) {
                console.log(c);
                //all_page_num为数据总量
                all_data_num = c.total
                //向上取整 max_page为最大页码
                max_page = Math.ceil(all_data_num/data_for_one_num)
                //if 判断 start
                if(c.code == -3000){
                $("#data-list").append(
                    alert("没有找到数据")
                );
                }
                //for i 循环 start
                for (let i = 0; i < data_for_one_num; i++) {
                    $("#data-list").append(
                        '<tr><td class="uuid">' + c.content[i].uuid + '</td><td class="dicgroup">' + c.content[i].dicgroup + '</td> <td class="dickey">'
                        + c.content[i].dickey + '</td><td class="dicvalue">' + c.content[i].dicvalue + '</td><td class="state">' + c.content[i].state + '</td> <td class="revision">' + c.content[i].revision + '</td>' +
                        '<td><a data-toggle="modal" class="modify-modal" href="#modify">修改 </a><a class="del"> 删除</a></td></tr>'
                    )
                }
            }
        })
    }

//ajax start 获取总量 与 第一页的显示
    $.ajax({
        url: page_data_url,
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            //offset:起始位置 = 指针
            //limit:获取条数 =默认为10
            limit: data_for_one_num,
            offset: Pointer,
        },
        success: function (e) {
            console.log(e);
            //all_page_num为数据总量
            all_data_num = e.total
            //向上取整 max_page为最大页码
            max_page = Math.ceil(all_data_num/data_for_one_num)
            //页面第一次加载时执行append
            append();
        }

    })

    //切换页面方法
    function page_change (to_page){
        if(to_page <=0){
            Pointer = 0
            pager_number = 1
            append()
            return '不能小于或等于0'
        }else if(to_page > 0){
            if(to_page > max_page){
                Pointer = (max_page * data_for_one_num) - data_for_one_num
                pager_number = max_page
                append()
                return "超过最大值"
            }
            else if (to_page <= max_page){
                Pointer = (to_page * data_for_one_num) - data_for_one_num
                //用户切换了页面 改变了指针位置 执行append方法
                pager_number = to_page
                append()
            }
        }
    }
    //按钮值判断
    $(".previous").click(function (){
        //if 判断 start
        if(pager_number <= 1){
        }
        else if (pager_number > 1){
            page_change(pager_number-1)
        }
    });
    $(".next").click(function (){
        //if 判断 start
        if(pager_number < max_page){
            page_change(pager_number+1)
        }
        else if (pager_number >= max_page){

        }
    });

//分页end

// 添加字典

$("#save_addkey").click(function (){
   let uuid = $("#uuid").val();
   let dicgroup = $("#dicgroup").val();
   let dickey = $("#dickey").val();
   let dicvalue = $("#dicvalue").val();
   let state = $("#state").val();
   let revision = $("#revision").val();
       //ajax start
           $.ajax({
               url: baseURL+'/admin/api/dic/addDic',
               type:'post',
               dataType: 'json',
               headers: {'authorities': authorities},
               data:{
                   dicgroup:dicgroup,
                   uuid:uuid,
                   dickey:dickey,
                   dicvalue:dicvalue,
                   state:state,
                   revision:revision,
               },
               success:function (e){
                   console.log(e.msg);
               }

           })
});
//查询字典
    $("#search_button").click(function (){
       let dicvalue =  $("#search_dicvalue").val();
       let dicgroup =  $("#search_dicgroup").val();
        append(dicvalue,dicgroup)
    });
//模态框
    $("#data-list").on("click", ".modify-modal", function () {
        $("#sn-modify").text($(this).parents('td').siblings(".uuid").text())
        $("#yw-modify").text($(this).parents('td').siblings(".dicgroup").text())
        $("#yy-modify").text($(this).parents('td').siblings(".dickey").text())
        $("#zz-modify").text($(this).parents('td').siblings(".dicvalue").text())
        $("#zw-modify").text($(this).parents('td').siblings(".state").text())
        $("#gg-modify").text($(this).parents('td').siblings(".revision").text())
    })
    $("#save_mod_change").click(function (){
       let uuid = $("#sn-modify").val();
       let dicgroup = $("#yw-modify").val();
       let dickey = $("#yy-modify").val();
       let dicvalue = $("#zz-modify").val();
        console.log(uuid);
        console.log(dicgroup);
        console.log(dickey);
        console.log(dicvalue);
        //ajax start
               $.ajax({
                   url: baseURL+'/admin/api/dic/editDic',
                   type:'post',
                   dataType: 'json',
                   headers: {'authorities': authorities},
                   data:{
                       dicId:uuid,
                       dickey:dickey,
                       dicvalue:dicvalue,
                       dicgroup:dicgroup,
                   },
                   success:function (e){
                       console.log(e);
                   }

               })
    })



})